using IM.Easy.Infrastructure.Query;
using Microsoft.AspNetCore.Mvc;
using IM.Easy.SystemServices.Dtos;
using IM.Easy.SystemServices.IServices;
using IM.Easy.Entity.System;
using IM.Easy.Core.Attributes;

namespace IM.Easy.Api.Controllers.System
{
    /// <summary>
    /// SysPostController 控制器
    /// </summary>
    [Route("system/post")]
    public class SysPostController : ControllerBase
    {
        private readonly ISysPostService _service;

        /// <summary>
        /// 构造函数，注入服务
        /// </summary>
        /// <param name="service">服务接口</param>
        public SysPostController(ISysPostService service)
        {
            _service = service;
        }

        /// <summary>
        /// 获取所有SysPost对象
        /// </summary>
        /// <returns>返回SysPost列表</returns>
        [HttpGet]
        [Api(Perm = "post:list", Name = "岗位列表")]
        public PageResponse<SysPost> GetList([FromQuery] SysPostQueryDto query)
        {
            return _service.GetPageList(query);
        }

        /// <summary>
        /// 根据ID获取SysPost对象
        /// </summary>
        /// <param name="id">对象的ID</param>
        /// <returns>返回指定的SysPost对象</returns>
        [HttpGet("{id}")]
        [Api(Perm = "post:info", Name = "岗位详情")]
        public SysPost GetById([FromRoute] long id)
        {
            var result = _service.GetById(id);
            return result ?? throw new KeyNotFoundException($"未找到ID为 {id} 的SysPost对象。");
        }

        /// <summary>
        /// 创建新的SysPost对象
        /// </summary>
        /// <param name="entity">要创建的SysPost对象</param>
        /// <returns>返回创建后的SysPost对象</returns>
        [HttpPost]
        [Api(Perm = "post:add", Name = "新增岗位")]
        public SysPost Create([FromBody] SysPost entity)
        {
            _service.Create(entity);
            return entity; // 直接返回创建的实体
        }

        /// <summary>
        /// 更新现有的SysPost对象
        /// </summary>
        /// <param name="id">对象的ID</param>
        /// <param name="entity">更新后的SysPost对象</param>
        [HttpPut]
        [Api(Perm = "post:put", Name = "更新岗位")]
        public void Update([FromBody] SysPost entity)
        {
            _service.UpdatePost(entity);
        }

        /// <summary>
        /// 批量删除SysPost对象
        /// </summary>
        /// <param name="ids">对象的IDS</param>
        [HttpDelete]
        [Api(Perm = "post:del", Name = "删除岗位")]
        public void Delete([FromBody] long[] ids)
        {
            _service.Delete(ids);
        }
    }
}
